#include <iostream>
using namespace std;

const int N = 1e5 + 10;
int color[N];


void Init(int n)
{
	for (int i = 0; i <= n; i++) 
		color[i] = i; 
}

int find(int a) //返回节点a的颜色
{
	return color[a];
}

int merge(int a, int b,int n) //联通操作,n表示总的节点数量
{
	int aa = find(a), bb = find(b);
	if (aa == bb) return 0;
	for (int i = 0; i <= n; i++)
	{
		if (color[i] == aa)
		{
			color[i] = bb;
		}
	}
	return 1;
}

void output(int n) //输出每个节点的颜色
{
	int ret = 0;
	for (int i = 0; i <= n; i++)
	{
		ret += printf("%3d", i);
	}

	cout << "\n";
	for (int i = 0; i < ret; i++)printf("-");
	cout << "\n";

	for (int i = 0; i <= n; i++) printf("%3d", color[i]);

	cout << "\n";

}


int main()
{
	int n;
	cin >> n;
	Init(n);
	int a, b;
	while (cin >> a >> b)
	{
		printf("merge %d with %d : %d\n", a, b, merge(a, b, n));
		output(n);
	}

	return 0;
}

//联通判断：O(1)
//合并操作：O（N)，实质上就是把一棵树作为另一棵树的子树




//数据测试
//10
//0 1
//9 10
//6 7
//8 6

